package com.itic.mobile.uc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;
import com.itic.mobile.base.BaseMobileInterface;
import com.itic.system.xhfeedback.persistence.model.XhFeedback;
import com.itic.system.xhfeedback.service.XhFeedbackService;
import com.itic.system.xhquerstion.persistence.model.XhQuerstion;
import com.itic.system.xhquerstion.persistence.model.XhQuerstionExample;
import com.itic.system.xhquerstion.service.XhQuerstionService;
import com.itic.system.xhuser.persistence.model.XhUser;
import com.itic.system.xhuserstore.persistence.model.XhUserStore;
import com.itic.system.xhuserstore.persistence.model.XhUserStoreExample;
import com.itic.system.xhuserstore.service.XhUserStoreService;


/**
 * 现货登入接口类
 * @author 许小宝
 * 2017年07月25日  14时11分
 */
@Controller
@RequestMapping(value="/mobile/uc")
public class UcInterface
     extends BaseMobileInterface {
	@Autowired
	private XhFeedbackService xhFeedbackService;
	@Autowired
	private XhQuerstionService xhQuerstionService;
	@Autowired
	private XhUserStoreService xhUserStoreService;
	 // 返回属性黑名单
    private static String[] ignoreProperties = new String[] { "createUser", "isEffective", "createTime",
            "orgCode" };
    /**
     * 发送验证码
     * @return
     */
    @RequestMapping(value="/index")
    @ResponseBody
    public Map<String,Object> send( HttpServletRequest request,HttpServletResponse response,String phone, String token ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(token) ){
    			XhUser user = getUserByToken(token);
    			if( null != user ){
    				if( StringHelper.isBlank(user.getHeadImg()) ){
    					user.setHeadImg(sysSettingService.getByKey("DEFAULT_HEAD_IMAGE"));
    				}
    				if( StringHelper.isBlank(user.getBackImg()) ){
    					user.setBackImg(sysSettingService.getByKey("DEFAULT_HEAD_IMAGE"));
    				}
    				if( StringHelper.isBlank(user.getUserName()) ){
    					user.setUserName(sysSettingService.getByKey("DEFAULT_USER_NAME"));
    				}
    				resultMap.put(DATA, ValueHelper.transBean2Map(user, null, ignoreProperties));
        			errorCode = SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG;
    			}else{
        			errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
    		logException("LoginInterface", "send", e.toString(),"用户注册发送验证码",e);
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    /**
     * 编辑用户名称
     * @return
     */
    @RequestMapping(value="/editName")
    @ResponseBody
    public Map<String,Object> editName(HttpServletRequest request,HttpServletResponse response, String token, String name ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    	    if( StringHelper.isNotBlank(token) ){
    	    	XhUser user = getUserByToken(token);
    	    	if( null != user ){
    	    		user.setUserName(name);
    	    		xhUserService.edit(user);
    	    		errorCode = SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG;
    	    	}else{
    	    		errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    	    	}
    	    }
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	logException("LoginInterface", "reg", e.toString(),"用户注册",e);
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    /**
     * 编辑用户头像
     * @return
     */
    @RequestMapping(value="/editHeadImg")
    @ResponseBody
    public Map<String,Object> editHeadImg(HttpServletRequest request,HttpServletResponse response, String token, String headImg ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    	    if( StringHelper.isNotBlank(token) ){
    	    	XhUser user = getUserByToken(token);
    	    	if( null != user ){
    	    		//user.setUserName(name);
    	    		user.setHeadImg(headImg);
    	    		xhUserService.edit(user);
    	    		errorCode = SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG;
    	    	}else{
    	    		errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    	    	}
    	    }
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	logException("LoginInterface", "reg", e.toString(),"用户注册",e);
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    /**
     * 编辑用户头像
     * @return
     */
    @RequestMapping(value="/editBackImg")
    @ResponseBody
    public Map<String,Object> editBackImg(HttpServletRequest request,HttpServletResponse response, String token, String backImg ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    	    if( StringHelper.isNotBlank(token) ){
    	    	XhUser user = getUserByToken(token);
    	    	if( null != user ){
    	    		//user.setUserName(name);
    	    		user.setBackImg(backImg);
    	    		xhUserService.edit(user);
    	    		errorCode = SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG;
    	    	}else{
    	    		errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    	    	}
    	    }
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	logException("LoginInterface", "reg", e.toString(),"用户注册",e);
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    /**
     * 问题反馈
     * @return
     */
    @RequestMapping(value="/feedbacks")
    @ResponseBody
    public Map<String,Object> feedbacks(HttpServletRequest request,HttpServletResponse response, String tokens, XhFeedback feedback ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    	    if( StringHelper.isNotBlank(tokens) && StringHelper.isNotBlank(feedback.getContent()) ){
    	    	XhUser user = getUserByToken(tokens);
    	    	if( null != user ){
    	    		feedback.setUserId(user.getId());
    	    		xhFeedbackService.add(feedback);
    	    		errorCode = SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG;
    	    	}else{
    	    		errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    	    	}
    	    }
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	logException("LoginInterface", "reg", e.toString(),"用户注册",e);
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    /**
     * 问题反馈
     * @return
     */
    @RequestMapping(value="/feedback")
    @ResponseBody
    public Map<String,Object> feedback(HttpServletRequest request,HttpServletResponse response, String token, XhFeedback feedback ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    	    if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(feedback.getContent()) ){
    	    	XhUser user = getUserByToken(token);
    	    	if( null != user ){
    	    		feedback.setUserId(user.getId());
    	    		xhFeedbackService.add(feedback);
    	    		errorCode = SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG;
    	    	}else{
    	    		errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG;
    	    	}
    	    }
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	logException("LoginInterface", "reg", e.toString(),"用户注册",e);
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    
    /**
     * 常见问题列表
     * @return
     */
    @RequestMapping(value="/questions")
    @ResponseBody
    public Map<String,Object> questions(HttpServletRequest request,HttpServletResponse response ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		Map<String, Object> dataMap = new HashMap<String,Object>();
    	    XhQuerstionExample example = new XhQuerstionExample();
    	    example.setOrderByClause("CREATE_TIME DESC");
    	    example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
    	    int count = xhQuerstionService.count(example);
    	    List<XhQuerstion> list = xhQuerstionService.list(example);
    	    String isMore = isMore(request,count);
    	    dataMap.put("questions", ValueHelper.tranList2MapList(list, ignoreProperties));
    	    dataMap.put("count",count);
    	    dataMap.put("isMore", isMore);
    	    resultMap.put(DATA, dataMap);
    	    errorCode = SUCCESS_ERROR_CODE;
        	errorMsg = SUCCESS_ERROR_MSG;
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	logException("LoginInterface", "reg", e.toString(),"用户注册",e);
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    /**
     * 常见问题列表
     * @return
     */
    @RequestMapping(value="/myStore")
    @ResponseBody
    public Map<String,Object> myStore( HttpServletRequest request,HttpServletResponse response, String token ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(token) ){
    			XhUser user = getUserByToken(token);
    			if( null != user ){
    				Map<String, Object> dataMap = new HashMap<String,Object>();
    	    		XhUserStoreExample example = new XhUserStoreExample();
    	    	    example.setOrderByClause("CREATE_TIME DESC");
    	    	    example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
    	    	    this.handlePageQuery(request, example);
    	    	    int count = xhUserStoreService.count(example);
    	    	    List<XhUserStore> list = xhUserStoreService.list(example);
    	    	    String isMore = isMore(request,count);
    	    	    dataMap.put("stores", ValueHelper.tranList2MapList(list, ignoreProperties));
    	    	    dataMap.put("count",count);
    	    	    dataMap.put("isMore", isMore);
    	    	    resultMap.put(DATA, dataMap);
    	    	    errorCode = SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG;
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	logException("LoginInterface", "reg", e.toString(),"用户注册",e);
    	}
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }
}